# -*- coding:utf-8 -*-
"""
Python 3.7
Author：刘爽、何若溥
Time：2022/08/25
"""

from datetime import datetime
from dataProcess.bugDBConn import BugDBConn
from common.chartUtil import getChart, getExcelPic
from common.fileUtil import getProperties, clearDir, getLocalPicPath, getRemotePicPath, getChartRemoteUrl
from common.ftpUtil import FtpUtil
from common.path import CONFIGPATH, PICPATH


def getDataCollect(project_id):
    # 清理文件夹
    clearDir(PICPATH)

    # 获取配置信息
    config = getProperties(CONFIGPATH)

    # 连接禅道数据库，获取Bug数据
    dbConn = BugDBConn(project_id, config["db_host"], config["db_user"], config["db_passwd"], int(config["db_port"]), config["db_name"])
    date = datetime.now().strftime('%Y%m%d%H%M%S')

    # 连接ftp服务器，准备上传
    ftp_conn = FtpUtil(config["ftp_host"], int(config["ftp_port"]), config["ftp_user"], config["ftp_passwd"])
    url_list = []
    td_new_bug_count, td_resolve_bug_count, td_unResolve_bug_count = dbConn.todayBugData()
    active_bug_list_data = dbConn.activeBugList()

    today_bug_list_name = f"今日Bug情况统计图_{date}"
    # today_bug_list_title = f"今日新增{td_new_bug_count}个;解决{td_resolve_bug_count}个;未解决{td_unResolve_bug_count}个（列表如下）"
    getExcelPic(active_bug_list_data, today_bug_list_name)
    ftp_conn.upload(getLocalPicPath(today_bug_list_name), getRemotePicPath(today_bug_list_name))
    url_list.append(getChartRemoteUrl(today_bug_list_name))
    td_bug = [td_new_bug_count, td_resolve_bug_count, td_unResolve_bug_count]

    status_data_x_list, status_data_y_list = dbConn.bugStatusData()
    severity_data_x_list, severity_data_y_list = dbConn.bugActiveSeverityData()
    severity_data_x_list = list(map(lambda x: "P"+str(x), severity_data_x_list))
    assign_data_x_list, assign_data_y_list = dbConn.bugActiveAssignData()

    x_list = [status_data_x_list, severity_data_x_list, assign_data_x_list]
    y_list = [status_data_y_list, severity_data_y_list, assign_data_y_list]
    y_name = "Bug数量（个）"
    chart_name_list = [f"Bug状态统计图_{date}", f"未解决Bug严重等级统计图_{date}", f"未解决Bug指派人统计图_{date}"]
    x_name_list = ["Bug状态", "Bug严重等级", "Bug指派给"]

    for i in range(0, 3):
        getChart(x_list[i], y_list[i], chart_name_list[i], x_name_list[i], y_name)
        ftp_conn.upload(getLocalPicPath(chart_name_list[i]), getRemotePicPath(chart_name_list[i]))
        url_list.append(getChartRemoteUrl(chart_name_list[i]))

    data_collect = {
        "urls": url_list,
        "tdBug": td_bug
    }
    dbConn.conn.closeDB()
    ftp_conn.close()
    return data_collect


